package tests.action;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Collections;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import action.Add;
import action.Edit;
import action.Find;

import storage.DataManager;
import storage.FileDatabase;
import data.Event;

public class EditTests
{

	private static Add addTestEvent = new Add();
	private static Find findEvent = new Find();
	static FileDatabase newdatabase = new FileDatabase();

	Edit editTestEvent = new Edit();
	private static ArrayList<Event> testCase1;
	private static ArrayList<Event> testCase2;
	private static ArrayList<Event> testCase3;
	private static ArrayList<Event> testCase1mod;
	private static ArrayList<Event> testCase2mod;
	private static ArrayList<Event> testCase3mod;
	private static Event case1;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		DataManager.loadLists();

		if (newdatabase.clearDatabase()) {
			System.out.println("db empty");
		}
		testCase1 = new ArrayList<Event>();
		Event[] result = addTestEvent.execute("add dance performance");
		Collections.addAll(testCase1, result);

		testCase1mod = new ArrayList<Event>();
		case1 = new Event("no dance today");
		testCase1mod.add(case1);

		testCase2 = new ArrayList<Event>();
		result = addTestEvent.execute("add break a leg performance at 5.30pm");
		Collections.addAll(testCase2, result);
		testCase2mod = new ArrayList<Event>();
		result = addTestEvent.execute("add break a leg performance at 5.30pm");
		Collections.addAll(testCase2mod, result);

		testCase3 = new ArrayList<Event>();
		result = addTestEvent.execute("add auditions were good today");
		Collections.addAll(testCase3, result);
		testCase3mod = new ArrayList<Event>();
		result = addTestEvent.execute("add auditions were good today");
		Collections.addAll(testCase3mod, result);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Test
	public void test() {

		Event[] random = new Event[1];

		random = findEvent.execute("dance performance");
		random = editTestEvent.execute("edit " + random[0].getId());
		random = editTestEvent.execute("edit no dance");

		ArrayList<Event> foundEvents1a = new ArrayList<Event>();
		ArrayList<Event> foundEvents1b = new ArrayList<Event>();
		Collections.addAll(foundEvents1a, findEvent.execute("no dance")); // should
																			// have
																			// event
		Collections.addAll(foundEvents1b,
				findEvent.execute("dance performance"));
		assertEquals(-1, foundEvents1b.indexOf(testCase1.get(0)));
		assertEquals(foundEvents1a.indexOf(testCase1mod.get(0)),
				foundEvents1a.lastIndexOf(testCase1mod.get(0)));

	}

}
